import os
import sys
parent_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
sys.path.append(parent_dir)

import json
import ast
import re
import fool

from utils.log import logger
from utils.chatglm_utils import ChatGLMUtils

class OthersRecognition:
    def __init__(self, data_source_path) -> None:
        data_dict = json.load(open(data_source_path, 'r', encoding='utf-8'))
        self.data_source = data_dict[0]
        self.id2members = {}
        self.id2field = {}

        for row in self.data_source['rows']:
            self.id2members[row['id']] = row['members']

        for hd in self.data_source['header']:
            self.id2field[hd['id']] = hd['fieldType']

    def filter(self, literals):
        """
        从literals字段中提取其他字面量的相关json
        :param literals: 字面量数据的json列表
        :return: 除了数值和时间之外其他相关的literals
        """
        other_literals = []
        try:
            for header in literals:
                # 字面量为空，属于时间维度，且为指标的情况下跳过
                if header["literal"] == "none" or header['timeLevel'] != "" or self.id2field[header['id']] == 'MEASURE':
                    continue
                else:
                    number_pattern = r"零|一|二|三|四|五|六|七|八|九|十|百|千|万|亿|0|1|2|3|4|5|6|7|8|9"
                    m = re.search(number_pattern, header["literal"])
                    if m is None:
                        other_literals.append(header)

            return other_literals

        except Exception as err:
            logger.error("other literal recognition error[{}]: {}".format(type(err), str(err)))
            raise


    def transform(self, query, other_literals):
        """
        格式化时间字面量
        param other_literals: 除数值和时间维度之外相关的literals
        return: conds中关于其他条件的部分
        """
        conds = []
        try:
            for header in other_literals:
                literal = header['literal']
                operator = '=='

                # 如果字面量在header包含的维度成员中存在
                if literal in self.id2members[header['id']]:
                    conds.append([header['id'], operator, literal])
                # 如果不存在，则分词并做模糊查询
                else:
                    words = fool.cut(literal)[0]
                    for member in self.id2members[header['id']]:
                        for word in words:
                            #万一有特殊符号会报错
                            m = re.search(f'[{word}]', member)
                            if m:
                                conds.append([header['id'], operator, member])
                                break

            return conds

        except Exception as err:
            logger.error("other literal recognition error[{}]: {}".format(type(err), str(err)))
            raise


if __name__ == "__main__":
    data_source_path = 'data/data_dict.json'
    other_reg = OthersRecognition(data_source_path)
    query = "去年12月合同金额大于500万的分部有多少？"
    literals = [
        {
            "id": "AUGMENTED_DATASET_LEVEL.I8a8ae5ca0178549554951b9501785cefe3f00058.MT_DIMENSION_FIELD.2b4ff0f606441075f12c1be466e3642e-LEVEL-1659584076544",
            "name":"销售分部",
            "operator":"none",
            "literal":"广州分部",
            "type" : "STRING",
            "timeLevel" : "",
            "member_amount" : 12
        },
        {
            "id": "AUGMENTED_DATASET_LEVEL.I8a8ae5ca0178549554951b9501785cefe3f00058.MT_DIMENSION_FIELD.3943e92bed35dac104c3a1f2b2195a5e-LEVEL-1667896190875",
            "name":"年",
            "operator":"==",
            "literal":"去年",
            "type" : "STRING",
            "timeLevel" : "year",
            "member_amount" : 1
        },
        {
            "id": "AUGMENTED_DATASET_LEVEL.I8a8ae5ca0178549554951b9501785cefe3f00058.MT_DIMENSION_FIELD.3943e92bed35dac104c3a1f2b2195a5e-LEVEL-1667896190875",
            "name":"月",
            "operator":"==",
            "literal":"去年12月",
            "type" : "STRING",
            "timeLevel" : "month",
            "member_amount" : 1
        },
        {
            "id": "AUGMENTED_DATASET_CALC_MEASURE.I8a8ae5ca0178549554951b9501785cefe3f00058.MT_METRICS.I8aaa80f7018022312231602d018044ba44ff0154",
            "name":"合同金额",
            "operator":">",
            "literal":"",
            "type" : "DOUBLE",
            "timeLevel" : "",
            "member_amount" : 0
        },
    ]
    other_literals = other_reg.filter(literals)
    print(other_reg.transform(query, other_literals))